//
//  MyBalanceViewController.m
//  BaishidaiKnight
//
//  Created by mac book on 2019/1/28.
//  Copyright © 2019年 sy. All rights reserved.
//

#import "MyBalanceViewController.h"
#import "BalanceDetailViewController.h"
#import "ApplicationCashViewController.h"



@interface MyBalanceViewController ()

@property(nonatomic,strong) UIView* headerView;
@property(nonatomic,strong) UIImageView* iconImageView;
@property(nonatomic,strong) UILabel* titleLabel;
@property(nonatomic,strong) UILabel* moneyLabel;
@property(nonatomic,strong) UIButton* detailBtn;
@property(nonatomic,strong) UIButton* appCashBtn;


@property(nonatomic,strong) UserInfoModel * userInfoModel;

@end

@implementation MyBalanceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"账户余额";
    [self creatSubViews];
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self queryData];
    
}
#pragma mark - 
-(void)creatSubViews{
    [self.view addSubview:self.appCashBtn];
    [self.view addSubview:self.headerView];
    
    
    [self.headerView addSubview:self.iconImageView];
    [self.headerView addSubview:self.titleLabel];
    [self.headerView addSubview:self.moneyLabel];
    [self.headerView addSubview:self.detailBtn];

    
    
    [self.appCashBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(50.f);
        make.right.equalTo(self.view);
        make.left.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
    
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.right.equalTo(self.view);
        make.left.equalTo(self.view);
        make.bottom.equalTo(self.appCashBtn.mas_top);
    }];
    
    
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view);
        make.top.mas_equalTo(100.0);
        make.size.mas_equalTo(CGSizeMake(75, 75));
    }];
    
    
    
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.iconImageView.mas_bottom).mas_offset(20);
        make.width.equalTo(self.headerView);
        make.left.equalTo(self.headerView);
        make.height.mas_equalTo(40);
        
    }];
    
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.moneyLabel.mas_bottom);
        make.left.right.equalTo(self.moneyLabel);
        make.height.equalTo(self.moneyLabel);
    }];
    
    
    [self.detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(20);
        make.left.right.equalTo(self.headerView);
        make.height.equalTo(self.titleLabel);
    }];
    
    
}

#pragma mark -  DataSource
-(void)queryData{
    
    NSDictionary* arg = @{
                          @"ince":@"get_emp_info",
                          @"emp_id":[UserHandle shareInstance].userId
                          };
    
    [BSDNetTool PostURL:kHost  parameters:arg succeed:^(NSDictionary *data) {
        NSLog(@"账户 = %@",data);
        NSInteger flag = [[data objectForKey:@"status"]integerValue];
        if(flag == 1){
            NSDictionary* emptyDict = [data objectForKey:@"info"];
            self.userInfoModel = [UserInfoModel mj_objectWithKeyValues:emptyDict];
            
            
            self.moneyLabel.text = [NSString stringWithFormat:@"%.2f",[self.userInfoModel.account floatValue]];

            
            
        }else{
            [self alertHUD:[data objectForKey:@"fail"]];
        }
        
    } failure:^(NSError *error) {
        [self alertHUD:@"网络异常"];
    }];
    
    
}

#pragma mark - 
-(void)detailTouch:(UIButton *)sender{
    BalanceDetailViewController* controller = [[BalanceDetailViewController alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}
-(void)appCashTouch:(UIButton *)sender{
    ApplicationCashViewController* controller = [[ApplicationCashViewController alloc]init];
    controller.userInfoModel = self.userInfoModel;
    [self.navigationController pushViewController:controller animated:YES];
}

#pragma mark -  property package
-(UIView *)headerView{
    if(!_headerView){
        _headerView = [[UIView alloc]init];
        _headerView.backgroundColor = [UIColor whiteColor];
        
    }
    return _headerView;
}

-(UIImageView *)iconImageView{
    if(!_iconImageView){
        _iconImageView = [[UIImageView alloc]init];
        [_iconImageView setImage:[UIImage imageNamed:@"icon_balance_cash"]];
    }
    return _iconImageView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.text = @"余额总数(元)";
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = kMyColor(64, 64, 64);
        _titleLabel.font = [UIFont systemFontOfSize:17.f];
    }
    return _titleLabel;
}

-(UILabel *)moneyLabel{
    if(!_moneyLabel){
        _moneyLabel = [[UILabel alloc]init];
        _moneyLabel.textAlignment = NSTextAlignmentCenter;
        _moneyLabel.textColor =  kMyColor(248, 162, 48);
        _moneyLabel.font = [UIFont boldSystemFontOfSize:24.f];
    }
    return _moneyLabel;
}

-(UIButton *)detailBtn{
    if(!_detailBtn){
        _detailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_detailBtn setTitleColor:kMyColor(248, 162, 48) forState:UIControlStateNormal];
        _detailBtn.titleLabel.font =[UIFont systemFontOfSize:17.f];
        [_detailBtn setTitle:@"查看余额明细" forState:UIControlStateNormal];
        _detailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [_detailBtn addTarget:self action:@selector(detailTouch:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _detailBtn;
}

-(UIButton *)appCashBtn{
    if(!_appCashBtn){
        _appCashBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_appCashBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_appCashBtn setTitle:@"申请提现" forState:UIControlStateNormal];
        _appCashBtn.titleLabel.font =[UIFont systemFontOfSize:17.f];
        [_appCashBtn addTarget:self action:@selector(appCashTouch:) forControlEvents:UIControlEventTouchUpInside];
        _appCashBtn.backgroundColor =  kMyColor(248, 159, 39);
        
    }
    return _appCashBtn;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
